define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'opinion/index',
                    add_url: 'opinion/add',
                    edit_url: 'opinion/edit',
                    del_url: 'opinion/del',
                    multi_url: 'opinion/multi',
                    table: 'opinion',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'opinion_id',
                sortName: 'opinion_id',
                columns: [
                    [
                        {field: 'opinion_id', title: __('Opinion_id'), operate: false},
                        {field: 'user_id', title: __('User_id'), operate: false},
                        {field: 'nick_name', title: __('Nick_name'), operate: false},
                        {field: 'phone', title: __('Phone')},
                        {field: 'content', title: __('Content')},
                        {field: 'reply', title: __('Reply'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        }
                    ]
                ]
            });

            $(table).on("click", "[data-id].btn-hf", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.prompt({title: __('请输入回复内容，50字内'), formType: 2}, function (value, index) {
                    $.post(url, {id: id, reply: value}, function (res) {
                        if (res.code == 0) {
                            Toastr.error(res.msg);
                        } else {
                            table.bootstrapTable('refresh', {});
                        }
                    });
                    Layer.close(index);
                });

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    var html = '-';
                    if (row.reply) {

                    } else {
                        html = '<a href="javascript:;" class="text-info btn-hf" data-id="' + row.opinion_id + '" data-url="opinion/reply">回复</a>';

                    }
                    return html;
                }
            },
        }
    };
    return Controller;
});